<template>
<view>
<!-- 有优惠券 -->
<view class="box1" v-if="isCoupon">
  <view class="container">
    <view class="noCoupon" @tap="noCoupon">不使用优惠券</view>
  </view>
  <!-- 优惠券列表 -->
  <view v-for="(item, index) in couponList" :key="index" class="couponList" @tap="changeCoupon" :data-index="index">
    <image src="/static/remao_shop/img/coupon.png"></image>
    <view class="couponBox">
      <view class="couponBoxTop">
        <view class="money">
          <text style="font-size:30rpx;">￥</text>
          <text style="font-size:50rpx;">{{item.discount}}</text>
        </view>
        <view class="name" style="font-size:36rpx;font-weight: 700">{{item.name}}</view>
      </view>
      <view class="couponBoxBottom">
        <view class="money">满{{item.min_amount}}可用</view>
        <view class="name" style="color:rgb(196, 196, 196)">{{item.start_time}}-{{item.end_time}}</view>
      </view>
    </view>
  </view>
</view>
<!-- 无优惠券 -->
<view class="box2" v-if="!isCoupon">
  <image src="/static/remao_shop/img/zanwu.png"></image>
  <view>暂无可用优惠</view>
</view>
</view>
</template>

<script>
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      userId: '',
      // 订单合计价格
      money: '',
      // 可用优惠券列表
      couponList: [],
      //微擎数据
      requestShopId: '',
      // 是否有可用优惠券判断
      isCoupon: 'false'
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    console.log(options);
    this.setData({
      userId: options.userId,
      money: options.money
    });
    this.getUnicodeid();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    // 获取微擎必须字段
    getUnicodeid() {
      var that = this;
      wx.getStorage({
        key: 'requestShopId',
        success: function (res) {
          console.log(res);
          that.setData({
            requestShopId: res.data
          });
          that.getcoupon();
        }
      });
    },

    // 获取优惠卷列表
    getcoupon: function () {
      let that = this;
      app.globalData.request({
        url: api.default.coupon_index,
        data: {
          uid: that.userId // uniacid: that.data.requestShopId.uniacid,
          // acid: that.data.requestShopId.acid,
          // version: that.data.requestShopId.version,
          // platform: 'wx'

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        success: res => {
          console.log(res.coupon); // 判断有无优惠券

          if (res.coupon.length == 0) {
            that.setData({
              couponList: '',
              isCoupon: false
            });
          } else {
            var listA = [];
            var listB = []; // 筛选是否未使用

            for (let a = 0; a < res.coupon.length; a++) {
              if (res.coupon[a].status == 4) {
                listA.push(res.coupon[a]);
              }
            }

            console.log(that.money); //筛选最低使用金额

            for (let a = 0; a < listA.length; a++) {
              let minNum = parseFloat(listA[a].min_amount);
              let shopsNum = parseFloat(that.money);
              console.log(minNum, shopsNum);

              if (minNum < shopsNum || minNum == shopsNum) {
                // console.log(listA[a])
                listB.push(listA[a]); // console.log(listB)
                // listB = listA
              }
            } // 是否有满足条件可用的优惠


            if (listB.length == 0) {
              that.setData({
                couponList: '',
                isCoupon: false
              });
            } else {
              that.setData({
                couponList: listB,
                isCoupon: true
              });
            }
          }
        },
        fail: function (res) {},
        complete: function (res) {}
      });
    },
    // 选择使用优惠券
    changeCoupon: function (e) {
      let that = this;
      console.log(e.currentTarget.dataset.index);
      let index = e.currentTarget.dataset.index;
      wx.setStorage({
        key: 'couponId',
        data: that.couponList[index].my_id
      });
      wx.navigateBack({
        delta: 1
      });
    },
    // 选择不使用优惠券
    noCoupon: function () {
      wx.setStorage({
        key: 'couponId',
        data: '0'
      });
      wx.navigateBack({
        delta: 1
      });
    }
  }
};
</script>
<style>
@import "./changeCoupon.css";
</style>